//
//  RmdStoryCell.m
//  Travel assistant
//
//  Created by 杨帆 on 16/3/10.
//  Copyright © 2016年 旅游助手. All rights reserved.
//

#import "RmdStoryCell.h"
#import "RmdCollectionCell.h"
#import <AFNetworking.h>
#import "StoryModel.h"
#import "StoryDetailViewController.h"


#define KWidth [[UIScreen mainScreen] bounds].size.width
#define KHeight [[UIScreen mainScreen] bounds].size.height

@interface RmdStoryCell ()

@property (nonatomic, strong) UICollectionViewFlowLayout *layout;
@property(nonatomic,strong)UICollectionView *collectionView;
@property(nonatomic,strong)NSMutableArray *dataArray;


@end


@implementation RmdStoryCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.layout = [[UICollectionViewFlowLayout alloc]init];
        self.layout.itemSize = CGSizeMake(0.46*KWidth, 0.37*KHeight);
        self.layout.minimumLineSpacing = 5;
        self.layout.minimumInteritemSpacing = 5;
        self.layout.sectionInset = UIEdgeInsetsMake(10,10, 10, 10);
        self.collectionView = [[UICollectionView alloc]initWithFrame:(CGRectMake(0, 0,KWidth, 0.75*KHeight))collectionViewLayout:self.layout];
        self.collectionView.backgroundColor = [UIColor colorWithRed:1 green:1 blue:0.95 alpha:1];
        self.collectionView.scrollEnabled = NO;
        
        self.collectionView.delegate = self;
        self.collectionView.dataSource = self;
        [self addSubview:self.collectionView];
        [self.collectionView registerClass:[RmdCollectionCell class] forCellWithReuseIdentifier:@"cell"];
        
        
    }
    
    
    return self;
}














//定义展示的UICollectionViewCell的个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataArray.count;
}
//定义展示的Section的个数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

//每个UICollectionView展示的内容
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    RmdCollectionCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    
    StoryModel *m = self.dataArray[indexPath.row];
    
    [cell.coverImageV sd_setImageWithURL:[NSURL URLWithString:m.index_cover] placeholderImage:nil];
    
    cell.titleLabel.text = m.index_title;
    
    [cell.userImageV sd_setImageWithURL:[NSURL URLWithString:m.user[@"avatar_s"]] placeholderImage:nil];
    
    cell.userNameLael.text = m.user[@"name"];
    
    
    return cell;
}


-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
  
    StoryModel *m = self.dataArray[indexPath.row];
    
    
    
    self.myBlock(m.spot_id);
    
    
    
}







//返回这个UICollectionView是否可以被选择
-(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}


- (void)setData:(NSMutableArray *)array{
    self.dataArray = array;
    
}
































- (void)awakeFromNib {
    
    
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
